// DrawPolygon
/*
绘制面
 */
class DrawPolygon {
  constructor(arg) {
    this.viewer = arg.viewer
    this.Cesium = arg.Cesium
    this.callback = arg.callback
    this._polygon = null //活动面
    this._polygonLast = null //最后一个面
    this._positions = [] //活动点
    this._entities_point = [] //脏数据
    this._entities_polygon = [] //脏数据
    this._polygonData = null //用户构造面
  }

  //返回最后活动面
  get polygon() {
    return this._polygonLast
  }

  //返回面数据用于加载面
  getData() {
    return this._polygonData
  }

  //加载面
  loadPolygon(data, option) {
    var $this = this
    /* return this.viewer.entities.add({
      polygon: {
        hierarchy: new $this.Cesium.PolygonHierarchy(data),
        clampToGround: true,
        show: true,
        fill: option.flag,
        material: $this.Cesium.Color.fromCssColorString(option.color),
        width: 3,
        outlineColor: $this.Cesium.Color.fromCssColorString(option.outLineColor),
        outlineWidth: 10,
        outline: !option.flag
      }
    }) */
    var polygon = ""
    if (option.flag) {
      polygon = this.viewer.dataSourceDisplay._defaultDataSource.entities.add({
        polygon: {
          hierarchy: new $this.Cesium.PolygonHierarchy(data),
          clampToGround: true,
          show: true,
          fill: option.flag,
          material: $this.Cesium.Color.fromCssColorString(option.color),
          width: 20,
          outlineColor: $this.Cesium.Color.fromCssColorString(option.outLineColor),
          outlineWidth: option.outlineWidth ? option.outlineWidth : 2,
          outline: !option.flag
        }
      })
    } else {
      data.push(data[0])
      polygon = this.viewer.dataSourceDisplay._defaultDataSource.entities.add({
        polyline: {
          positions: data,
          show: true,
          material: $this.Cesium.Color.fromCssColorString(option.outLineColor),
          width: option.outlineWidth ? option.outlineWidth : 2,
          clampToGround: true
        }
      })
    }
    return polygon;
  }

  //开始绘制
  startCreate(option) {
    var $this = this
    this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.cesiumViewer.scene.canvas)
    this.handler.setInputAction(function (evt) { //单机开始绘制
      var cartesian = $this.getCatesian3FromPX(evt.position)
      if (!cartesian) return
      if ($this._positions.length == 0) {
        $this._positions.push(cartesian.clone())
      }
      $this.createPoint(cartesian)
      $this._positions.push(cartesian)
    }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK)
    this.handler.setInputAction(function (evt) { //移动时绘制面
      if ($this._positions.length < 1) return
      var cartesian = $this.getCatesian3FromPX(evt.endPosition)
      if (!cartesian) return
      if ($this._positions.length == 3) {
        if (!$this.Cesium.defined($this._polygon)) {

          $this._polygon = $this.createPolygon(option)
        }
      }
      $this._positions.pop()
      $this._positions.push(cartesian)
    }, $this.Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    this.handler.setInputAction(function (evt) {
      if (!$this._polygon) return
      var cartesian = $this.getCatesian3FromPX(evt.position)
      if (!cartesian) return
      $this._positions.pop()
      $this._positions.push(cartesian)
      $this.createPoint(cartesian)
      $this._polygonData = $this._positions.concat()
      $this.viewer.dataSourceDisplay._defaultDataSource.entities.remove($this._positions) //移除
      $this._polygon = null
      $this._positions = []
      var Polygon = $this.loadPolygon($this._polygonData, option)
      $this._entities_polygon.push(Polygon)
      $this._polygonLast = Polygon
      if (typeof $this.callback == 'function') {
        $this.callback(Polygon, $this._polygonData)
      }
      $this.clearPoint()
    }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK)
  }

  //创建面
  createPolygon(option) {
    var $this = this
    var polygon = this.viewer.dataSourceDisplay._defaultDataSource.entities.add({
      polygon: {
        hierarchy: new $this.Cesium.CallbackProperty(function () {
          return new $this.Cesium.PolygonHierarchy($this._positions)
        }, false),
        clampToGround: true,
        show: true,
        material: $this.Cesium.Color.fromCssColorString(option.color),
        width: 3,
        outlineColor: $this.Cesium.Color.fromCssColorString(option.outLineColor),
        outlineWidth: option.outlineWidth ? option.outlineWidth : 2,
        fill: option.flag,
        outline: !option.flag
      }
    })
    $this._entities_polygon.push(polygon)
    return polygon
  }

  //创建点
  createPoint(cartesian) {
    var $this = this
    var point = this.viewer.dataSourceDisplay._defaultDataSource.entities.add({
      position: cartesian,
      point: {
        pixelSize: 10,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
        color: $this.Cesium.Color.YELLOW
      }
    })
    $this._entities_point.push(point)
    return point
  }


  clearPoint() {
    for (var i = 0; i < this._entities_point.length; i++) {
      this.viewer.dataSourceDisplay._defaultDataSource.entities.remove(this._entities_point[i])
    }
    this._entities_point = [] //脏数据
  }

  //销毁事件
  destroy() {
    if (this.handler) {
      this.handler.destroy()
      this.handler = null
    }
  }

  //清空实体对象
  clear() {
    for (var i = 0; i < this._entities_point.length; i++) {
      this.viewer.dataSourceDisplay._defaultDataSource.entities.remove(this._entities_point[i])
    }
    for (var i = 0; i < this._entities_polygon.length; i++) {
      this.viewer.dataSourceDisplay._defaultDataSource.entities.remove(this._entities_polygon[i])
    }
    this._polygon = null //活动面
    this._polygonLast = null //最后一个面
    this._positions = [] //活动点
    this._entities_point = [] //脏数据
    this._entities_polygon = [] //脏数据
    this._polygonData = null //用户构造面
  }

  getCatesian3FromPX(px) {
    var cartesian
    var ray = this.viewer.cesiumViewer.camera.getPickRay(px)
    if (!ray) return null
    cartesian = this.viewer.cesiumViewer.scene.globe.pick(ray, this.viewer.cesiumViewer.scene)
    if (!cartesian) {
      cartesian = this.viewer.cesiumViewer.camera.pickEllipsoid(px, this.viewer.cesiumViewer.scene.globe.ellipsoid)
    }
    return cartesian
  }
}

export default DrawPolygon
